package com.littlesandbox.scale_cal;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.content.Intent;

public class MainActivity extends Activity
{ 
    public static Context ctx;
    public TextView ta;
    public TextView tb;
    public TextView result;
    //计算次数
    public int count =0;
    public LinearLayout history_view;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);  
        ta=findViewById(R.id.A);
        tb=findViewById(R.id.B);
        ctx=getApplicationContext();
        history_view=findViewById(R.id.history_view);
        result  = findViewById(R.id.result);
    }
    public void market(View v)
    {
        Intent i = new Intent(MainActivity.this,MarketCal.class);
        startActivity(i);
    }
    
    public void caculate(int p_A,int p_B)
    {
        int cal_A  =p_A;
        int cal_B =p_B;
        count +=1;
    //    Toast.makeText(ctx,"都是偶数,A:"+cal_A+"B:"+cal_B,Toast.LENGTH_LONG).show();
        //检测是否都是偶数
        if(cal_A%2==0&&cal_B%2==0)
        {            
           Toast.makeText(ctx,"都是偶数,A:"+cal_A+"B:"+cal_B,Toast.LENGTH_LONG).show();
           cal_A=cal_A/2;
            cal_B=cal_B/2;
            //     history.A_array.add(p_A);
            //  history.B_Array.add(p_B);
            add_history(cal_A,cal_B);
            caculate(cal_A,cal_B);       
        }
        //为奇数的处理方法 加上是否为1判断,避免任何数除以1都能整除的堆栈溢出问题
        else if(cal_B%cal_A ==0 &&cal_A!=1)
        {
         //   count +=1;
            cal_A /=cal_A;
            cal_B /=cal_A;
            add_history(cal_A,cal_B);
            //如果大数能够整除小数 且没有余数
              caculate(cal_A,cal_B);           
        }
        //如果除不尽了
        else
        {
            result.setText("结果比值:"+cal_A+":"+cal_B);
        }
    }
    public void add_history(int cal_A,int cal_B)
    {
        TextView t_count =new TextView(this);
        t_count.setText("第"+count+"次");
        t_count.setTextColor(Color.WHITE);
        TextView tmp_a = new TextView(this);
        TextView tmp_b = new TextView(this);
        tmp_a.setTextColor(Color.WHITE);
        tmp_b.setTextColor(Color.WHITE);
        tmp_a.setText("A:"+String.valueOf(cal_A));
        tmp_b.setText("B:"+String.valueOf(cal_B));
        history_view.addView(t_count);
        history_view.addView(tmp_a);
        history_view.addView(tmp_b);        
    }
    //清空历史记录
    public void cls(View v)
    {
        count=0;
        result.setText("结果:");
        history_view.removeAllViews();
        TextView t =new TextView(this);
        t.setText("历史计算结果记录");
        t.setTextColor(Color.RED);
        t.setBackgroundColor(Color.WHITE);
        history_view.addView(t);
    }
    public void about(View v)
    {
        AlertDialog.Builder ab =new AlertDialog.Builder(this);
        ab.setTitle("比例计算器");
        ab.setMessage("由小沙盒工作室室长开发,\n 版本alpha_v0.1,欢迎报bug,\n qq:2439905184,目前版本完成了求偶数的比例问题");
        ab.setNeutralButton("确定",null);
        ab.show();
    }
	public void cal(View v)
    {
        String stra = null;
        String strb = null;
        if(ta.getText().length()!=0&&tb.getText().length()!=0)
        {
            stra=ta.getText().toString();
            strb=tb.getText().toString();
            int A =Integer.decode(stra);
            int B =Integer.decode(strb);
            caculate(A,B);
            //        Toast.makeText(ctx,"A的值"+A,Toast.LENGTH_SHORT).show();
            //      Toast.makeText(ctx,"B的值"+B,Toast.LENGTH_SHORT).show();
        } else
        {
            Toast.makeText(getApplicationContext(),"错误,你没有输入参数",Toast.LENGTH_LONG).show();
        }
    }
} 
